package com.yijushequ.action;

import com.opensymphony.xwork2.ActionContext;
import com.yijushequ.dao.BaseUserDao;
import com.yijushequ.domain.BaseUser;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Locale;

@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Results({
        @Result(name = BasicAction.INPUT, type = "tiles", location = ".index"),
        @Result(name = BasicAction.SUCCESS, type = "tiles", location = ".index")
})
public class IndexAction extends BasicAction {


    private BaseUserDao baseUserDao;

    private BaseUser baseUser;


    String logInfo = "";
    private String language;

    @Action(value = "deployBeta",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".listAllContent")}
    )
    public String deployBeta() throws Exception {
        String filePath = "deploy.bat";
        String realPath = ServletActionContext.getServletContext().getRealPath(filePath);
        String cmd = "cmd /c start " + realPath;
        Process ps = Runtime.getRuntime().exec(cmd);
        return SUCCESS;
    }

    @Action(value = "viewLog",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".listAllContent")}
    )
    public String viewLog() throws Exception {
        logInfo = loadLog("D:\\dev\\WorkSpace\\yijushequ\\log\\buildLog.txt");
        return SUCCESS;
    }

    public static String loadLog(String filePath) throws IOException {

        String lineBreak = "<tr><td>";
        String lineBreak1 = "</td></tr>";
        File f = new File(filePath);
        BufferedReader br = null;
        String ret = null;
        try {
            br = new BufferedReader(new FileReader(f));
            String line = null;
            StringBuffer sb = new StringBuffer((int) f.length());
            while ((line = br.readLine()) != null) {
                sb.append(lineBreak).append(line).append(lineBreak1);
            }
            ret = sb.toString();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                }
            }
        }
        return ret;
    }


    @Action(value = "index",
            results = {@Result(name = SUCCESS, type = "tiles", location = ".index")}
    )
    public String index() {
        if ("en_US".equals(language)) {
            ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE", new Locale("en", "US"));
            ActionContext.getContext().getSession().put("request_locale", new Locale("en", "US"));
            ActionContext.getContext().getSession().put("request_only_locale", new Locale("en", "US"));
            ActionContext.getContext().setLocale(new Locale("en", "US"));
        }

        return SUCCESS;
    }


    public BaseUser getBaseUser() {
        return baseUser;
    }

    public void setBaseUser(BaseUser baseUser) {
        this.baseUser = baseUser;
    }

    public BaseUserDao getBaseUserDao() {
        return baseUserDao;
    }

    public void setBaseUserDao(BaseUserDao baseUserDao) {
        this.baseUserDao = baseUserDao;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getLogInfo() {
        return logInfo;
    }

    public void setLogInfo(String logInfo) {
        this.logInfo = logInfo;
    }


}
